import http from '@/api/http'
import type {
    APIResult,
    APIPaging,
    PaginationParams,
    PurchaseOrderDTO,
    PurchaseCreateParams,
    PurchaseQueryParams
} from '@/api/types'

export const purchaseAPI = {
    // 获取采购单列表
    getOrderList: (
        params: PaginationParams & PurchaseQueryParams
    ): Promise<APIResult<APIPaging<PurchaseOrderDTO>>> => {
        return http.get('/purchase/orders', { params })
    },

    // 获取采购单详情
    getOrderDetail: (orderId: string): Promise<APIResult<PurchaseOrderDTO>> => {
        return http.get(`/purchase/orders/${orderId}`)
    },

    // 创建采购单
    createOrder: (data: PurchaseCreateParams): Promise<APIResult<string>> => {
        return http.post('/purchase/orders', data)
    },

    // 提交审批
    submitOrder: (orderId: string): Promise<APIResult<void>> => {
        return http.post(`/purchase/orders/${orderId}/submit`)
    },

    // 审批操作
    approveOrder: (
        orderId: string,
        action: 'approve' | 'reject',
        comment?: string
    ): Promise<APIResult<void>> => {
        return http.post(`/purchase/orders/${orderId}/approve`, { action, comment })
    },

    // 更新收货数量
    updateReceivedQuantity: (
        orderId: string,
        itemId: string,
        receivedQuantity: number
    ): Promise<APIResult<void>> => {
        return http.patch(`/purchase/orders/${orderId}/items/${itemId}`, {
            receivedQuantity
        })
    },

    // 获取待审批列表
    getPendingApprovals: (): Promise<APIResult<PurchaseOrderDTO[]>> => {
        return http.get('/purchase/pending-approvals')
    }
}